package com.tianma315.admin.stock.view;

import com.tianma315.admin.check.service.CheckRecordService;
import com.tianma315.admin.check.vo.CheckRecordArrtVO;
import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.material.domain.PurchaseDO;
import com.tianma315.admin.stock.domain.WarehousingRecord;
import com.tianma315.admin.stock.domain.vo.WarehousingRecordVo;
import com.tianma315.admin.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
//======`-.____`-.___\_____/___.-`____.-'======
//                   `=---='
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//         佛祖保佑       永无BUG

/**
 * 库存视图
 * <p>
 * Created by zcm on 2019/5/28.
 */
@Controller
@RequestMapping("/stock/view")
public class StockViewController extends BaseController {

    @Autowired
    private CheckRecordService checkRecordService;
    @Autowired
    private StockService stockService;

    @GetMapping("/list")
    ModelAndView list() {
        ModelAndView view = new ModelAndView("stock/list");
        return view;
    }

    @GetMapping("/stockList")
    ModelAndView stockList() {
        ModelAndView view = new ModelAndView("stock/stockList");
        return view;
    }


    @GetMapping("/add")
    ModelAndView add() {
        ModelAndView view = new ModelAndView("stock/add");
        return view;
    }

    @GetMapping("/edit/{warehousingRecordId}")
    ModelAndView edit(@PathVariable("warehousingRecordId") Integer warehousingRecordId,Model model) {
        WarehousingRecordVo warehousingRecordVo = stockService.getWarehousingRecordById(warehousingRecordId);
         model.addAttribute("warehousingRecordVo",warehousingRecordVo);
        ModelAndView view = new ModelAndView("stock/edit");
        return view;
    }

    @GetMapping("/to_addReportImg/{warehousingRecordId}")
    String toAddReportImg(@PathVariable("warehousingRecordId")Integer warehousingRecordId, Model model){
        model.addAttribute("id",warehousingRecordId);
        return "stock/uploadview";
    }

    @GetMapping("/to_updateReportImg/{warehousingRecordId}")
    String toUpdateReportImg(@PathVariable("warehousingRecordId")Integer warehousingRecordId,Model model){
        WarehousingRecord warehousingRecord = stockService.selectById(warehousingRecordId);
        String[] imgUrl = warehousingRecord.getImgUrl().split(",");
        List<String> imgUrls  = new ArrayList<>(Arrays.asList(imgUrl));
        model.addAttribute("imgUrls",imgUrls);
        model.addAttribute("warehousingRecordId",warehousingRecordId);
        return "stock/updateimg";
    }

    @GetMapping("/add/{id}/{checkTemplateId}/{productName:.+}")
    String addCheckRecordAndArrt(@PathVariable("id")Integer id,
                                 @PathVariable("checkTemplateId")Integer checkTemplateId,
                                 @PathVariable("productName")String productName,
                                 Model model){
        model.addAttribute("warehousingRecordId",id);
        model.addAttribute("checkTemplateId",checkTemplateId);
        model.addAttribute("productName",productName);
        model.addAttribute("userName",getUser().getUsername());
        return "check/checkRecord/addCheckRecordAndArrt";
    }

    /**
     * 获取检测记录
     */
    @GetMapping("/getCheckRecordArrtVO/{warehousingRecordId}")
    String getCheckRecordArrtVO(@PathVariable("warehousingRecordId")Integer warehousingRecordId,Model model){
        CheckRecordArrtVO checkRecordAndArrtVO = checkRecordService.getCheckRecordArrtVOWarehousingRecordId(warehousingRecordId);
        model.addAttribute("checkRecordAndArrtVO",checkRecordAndArrtVO);
        return "check/checkRecord/checkRecordArrt";
    }

    /**
     * 进入导入excel
     */
    @GetMapping( "/stockImport")
    public String stockImport(MultipartFile file){
        return "stock/stockImport";
    }

    @GetMapping( "/statisticsList")
    public String statisticsList(MultipartFile file){
        return "stock/statisticsList";
    }
}
